package cn.tedu.user.management.controller;

import cn.tedu.rtms.commons.security.LoginPrincipal;
import cn.tedu.rtms.commons.web.JsonResult;
import cn.tedu.user.management.pojo.param.UserRoleUpdateParam;
import cn.tedu.user.management.pojo.vo.RoleListItemVO;
import cn.tedu.user.management.service.IRoleService;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.validation.Valid;
import java.util.List;

/**
 * 处理用户相关请求的控制器
 *
 * @author java@tedu.cn
 * @version 0.0.1
 */
@Slf4j
@RestController
@RequestMapping("/roles")
@Validated
@Api(tags = "04. 角色管理模块")
public class RoleController {
    @Autowired
    IRoleService roleService;
    // http://localhost:9250/roles/roleList
    @GetMapping("/roleList")
    @ApiOperation("查询角色列表")
    @ApiOperationSupport(order = 400)
    public JsonResult<List<RoleListItemVO>> roleList() {
        log.debug("开始处理【查询角色列表】的请求");
        List<RoleListItemVO> list = roleService.roleList();
        return JsonResult.ok(list);
    }

    // http://localhost:9250/roles/9527/update
    @PostMapping("/{id:[0-9]+}/update")
    @PreAuthorize("hasAuthority('/roles/update')")
    @ApiOperation("修改员工角色")
    @ApiOperationSupport(order = 300)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "员工ID", required = true, dataType = "long")
    })
    public JsonResult<Void> updateInfoById(@PathVariable @Range(min = 1, message = "请提交有效的员工ID值！") Long id,
                                           @Valid UserRoleUpdateParam userRoleUpdateParam,
                                           @ApiIgnore @AuthenticationPrincipal LoginPrincipal loginPrincipal) {
        log.debug("开始处理【修改员工角色】的请求，ID：{}，新数据：{}", id, userRoleUpdateParam);
        log.debug("当事人：{}", loginPrincipal);
        roleService.updateRoleById(id, userRoleUpdateParam, loginPrincipal.getId());
        return JsonResult.ok();
    }


}